package com.base.cn.platform.os.controller.order.goods;

import com.base.cn.platform.os.common.AdminBaseController;
import com.base.cn.platform.os.common.log.aop.AdminModuleEnum;
import com.base.cn.platform.os.common.log.aop.OpLog;
import com.base.cn.platform.os.common.utils.order.PayType;
import com.base.cn.platform.os.entity.order.order.OrderCondition;
import com.base.cn.platform.os.entity.order.order.OrderType;
import com.base.cn.platform.os.service.manage.order.goods.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台订单管理
 *
 * @author s.li
 * @create 2018-05-10-13:58
 */
@Controller
@RequestMapping("/admin/order")
public class AdminOrderController extends AdminBaseController {

    @Autowired
    private OrderService orderService;

    /**
     * 初始化全部订单数据列表
     * @return 列表页面
     */
    @RequestMapping("/queryOrderList")
    public String orderList(){
        return resultPage("/order/goods/order-list");
    }

    /**
     * 加载订单数据
     * @param model
     * @param params 查询条件
     * @param currentPage 页码
     * @return 显示页面
     */
    @RequestMapping("/ajaxQueryOrderList")
    public String ajaxQueryOrderList(Model model,
                                     @RequestBody Map<String, Object> params,
                                     @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        params.put("orderType", OrderType.COMMON.getKey());
        Map<String,Object> dataMap = orderService.findOrderPage(params,currentPage,true,false,false,true);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/order/goods/ajax-order-list");
    }

    /**
     * 初始充值订单数据列表
     * @return 列表页面
     */
    @RequestMapping("/queryUserAccountOrderList")
    public String queryUserAccountOrderList(){
        return resultPage("/order/goods/user-account-order-list");
    }
    /**
     * 加载充值订单数据
     * @param model
     * @param params 查询条件
     * @param currentPage 页码
     * @return 显示页面
     */
    @RequestMapping("/ajaxQueryUserAccountOrderList")
    public String ajaxQueryUserAccountOrderList(Model model,
                                     @RequestBody Map<String, Object> params,
                                     @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        params.put("orderType", OrderType.ACCOUNT.getKey());
        Map<String,Object> dataMap = orderService.findOrderPage(params,currentPage,true,false,false,false);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/order/goods/ajax-user-account-order-list");
    }

    /**
     * 初始化赠送订单数据列表
     * @return 列表页面
     */
    @RequestMapping("/queryFreeOrderList")
    public String orderFreeList(){
        return resultPage("/order/goods/free-order-list");
    }

    /**
     * 加载赠送订单数据
     * @param model
     * @param params 查询条件
     * @param currentPage 页码
     * @return 显示页面
     */
    @RequestMapping("/ajaxQueryFreeOrderList")
    public String ajaxQueryFreeOrderList(Model model,
                                     @RequestBody Map<String, Object> params,
                                     @RequestParam(value = "currentPage",required = false,defaultValue = "1") int currentPage){
        params.put("payTypeList","'"+PayType.PAY_TYPE_ADMIN_FREE+"','"+PayType.PAY_TYPE_MEMBER+"','"+PayType.PAY_TYPE_COURSE_CART+"'");//只查询赠送的
        Map<String,Object> dataMap = orderService.findOrderPage(params,currentPage,true,false,false,false);
        model.addAttribute("dataMap",dataMap);
        return resultPage("/order/goods/ajax-free-order-list");
    }

    /**
     * 订单详情
     * @param id 订单ID
     * @return 详情页面
     */
    @RequestMapping("/showOrderInfo")
    public String showOrderInfo(Model model,
                                @RequestParam("id")BigDecimal id){
        OrderCondition condition = new OrderCondition();
        condition.setId(id);
        condition.setSetUserData(true);
        condition.setSetDetailsData(true);
        condition.setSetShopData(true);
        condition.setSetOrderBook(true);
        Map<String,Object> order = orderService.findOrderInfo(condition);
        model.addAttribute("order",order);
        return resultPage("/order/goods/order-info");
    }

    /**
     * 修改订单状态
     * @param id 订单ID
     * @param orderStatus 订单状态
     * @param payType 支付类型（用于审核通过订单）
     * @param externalOrderNo 第三方流水号（用于审核通过订单）
     * @param mailName 物流公司
     * @param mailCode 物流单号
     * @param mailTime 发货时间
     * @return Map<String,Object>
     */
    @RequestMapping("/updateOrderStatus")
    @ResponseBody
    @OpLog(module = AdminModuleEnum.ORDER, action = "修改订单状态")
    public Map<String,Object> updateOrderStatus(HttpServletRequest request,
                                                @RequestParam("id") BigDecimal id,
                                                @RequestParam("orderStatus") String orderStatus,
                                                @RequestParam(value = "payTime",required = false) String payTime,
                                                @RequestParam(value = "payType",required = false) String payType,
                                                @RequestParam(value = "externalOrderNo",required = false) String externalOrderNo,
                                                @RequestParam(value = "mailName",required = false) String mailName,
                                                @RequestParam(value = "mailCode",required = false) String mailCode,
                                                @RequestParam(value = "mailTime",required = false) String mailTime) throws Exception{
        BigDecimal sysUserId = loginUtil.getLoginUserId(request);
        return orderService.updateOrderStatus(sysUserId,id,orderStatus,payTime,payType,externalOrderNo,mailName,mailCode,mailTime);
    }

    /**
     * 审核订单窗口
     * @return
     */
    @RequestMapping("/auditionOrderPage")
    public String auditionOrderPage(){
        return resultPage("/order/goods/auditing-order");
    }

    /**
     * 审核订单窗口
     * @return
     */
    @RequestMapping("/sendOutOrder")
    public String sendOutOrder(){
        return resultPage("/order/goods/send-out-order");
    }

    /**
     * 导出订单
     * @param keyword
     * @param orderStatus
     * @param payType
     * @param beginCreateTime
     * @param endCreateTime
     * @param beginPayTime
     * @param endPayTime
     * @return
     * @throws Exception
     */
    @RequestMapping("/downloadExcelOrdersList")
    @OpLog(module = AdminModuleEnum.ORDER, action = "导出全部订单")
    public ResponseEntity<byte[]> downloadExcelOrdersList(@RequestParam(value = "keyword",required = false) String keyword,@RequestParam(value = "orderStatus",required = false) String orderStatus,@RequestParam(value = "payType",required = false) String payType,
                                                          @RequestParam(value = "beginCreateTime",required = false) String beginCreateTime,@RequestParam(value = "endCreateTime",required = false) String endCreateTime,@RequestParam(value = "beginPayTime",required = false) String beginPayTime,
                                                          @RequestParam(value = "endPayTime",required = false) String endPayTime
                                                            ,@RequestParam(value = "gainDay",required = false) Integer gainDay ,@RequestParam(value = "spread",required = false) Integer spread,@RequestParam(value = "shopName",required = false) String shopName) throws Exception {
       Map<String,Object> params = new HashMap<>();
        params.put("keyword",keyword);
        params.put("orderStatus",orderStatus);
        params.put("payType",payType);
        params.put("spread",spread);
        params.put("beginCreateTime",beginCreateTime);
        params.put("endCreateTime",endCreateTime);
        params.put("beginPayTime",beginPayTime);
        params.put("endPayTime",endPayTime);
        params.put("gainDay",gainDay);
        params.put("shopNames",shopName);
        params.put("orderType", OrderType.COMMON.getKey());
        return orderService.downloadExcelOrdersList(params,2000);
    }

    /**
     * 推广的订单
     * @return 列表页面
     */
    @RequestMapping("/queryOrderSpreadList")
    public String orderSpreadList(){
        return resultPage("/order/goods/order-spread-list");
    }
}
